package cn.dankal.flyaudio.adapter;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.concurrent.TimeUnit;

import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewAdapter;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewHolder;
import cn.dankal.basiclib.pojo.ProductEntity;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.store.ProductDetailActivity;
import io.reactivex.functions.Consumer;

public class StoreProductAdapter extends BaseRecyclerViewAdapter<ProductEntity> {
    @Override
    protected int getLayoutResId(int viewType) {
        return R.layout.item_store_product;
    }

    private String type;

    public void setType(String type) {
        this.type = type;
    }

    @Override
    protected BaseRecyclerViewHolder getViewHolder(ViewGroup parent, View rootView, int viewType) {
        return new ViewHolder(rootView);
    }

    public class ViewHolder extends BaseRecyclerViewHolder<ProductEntity> {

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void onBindData(ProductEntity data, int position) {
            setText(R.id.tv_product_name,data.getProductName());
            setText(R.id.tv_product_price,"¥ "+data.getShowPrice());
            setImageUrl(R.id.iv_product_img,data.getMainImg());
            RxView.clicks(itemView.findViewById(R.id.iv_join)).throttleFirst(1, TimeUnit.SECONDS)
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if(type.equals("0")){
                                LiveEventBus.get("StoreProductJoin",ProductEntity.class).post(data);
                            }else if(type.equals("1")){
                                LiveEventBus.get("StoreProductJoin1",ProductEntity.class).post(data);
                            }else if(type.equals("2")){
                                LiveEventBus.get("StoreProductJoin2",ProductEntity.class).post(data);
                            }
                        }
                    });
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getContext(),ProductDetailActivity.class);
                    intent.putExtra("uuid",data.getUuid());
                    getContext().startActivity(intent);
                }
            });
        }
    }
}
